package com.gerfmarquez.nymph;

import static com.gerfmarquez.nymph.music.core.MusicCore.A;
import static com.gerfmarquez.nymph.music.core.MusicCore.As;
import static com.gerfmarquez.nymph.music.core.MusicCore.B;
import static com.gerfmarquez.nymph.music.core.MusicCore.C;
import static com.gerfmarquez.nymph.music.core.MusicCore.Cs;
import static com.gerfmarquez.nymph.music.core.MusicCore.D;
import static com.gerfmarquez.nymph.music.core.MusicCore.Ds;
import static com.gerfmarquez.nymph.music.core.MusicCore.E;
import static com.gerfmarquez.nymph.music.core.MusicCore.F;
import static com.gerfmarquez.nymph.music.core.MusicCore.Fs;
import static com.gerfmarquez.nymph.music.core.MusicCore.G;
import static com.gerfmarquez.nymph.music.core.MusicCore.Gs;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioTrack;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.gerfmarquez.nymph.R;
import com.gerfmarquez.nymph.music.core.MusicCore;
import com.gerfmarquez.nymph.music.core.PlayNotesThread;
import com.gerfmarquez.nymph.preferences.MusicPreferences;

public class TestNotesDurationActivity extends Activity {
	
	SharedPreferences shpr;
    static final String TAG= "TestNotesDurationActivity";
    byte[][] notesToPlay;
    byte[][] notesToPlayTwelve;

    public ArrayList<AudioTrack> audioTracks = new ArrayList<AudioTrack>();

 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 		MenuInflater inflater = getMenuInflater();
 		inflater.inflate(R.menu.menu, menu);
 		return true;
 	}
 	@Override
 	public void onStop() {
 		super.onStop();
 		for(AudioTrack aud: audioTracks) {
 			aud.release();
 		}
 	}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        MusicCore.init(this);
        
       
    }
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// We have only one menu option
		case R.id.preferences:
			// Launch Preference activity
			Intent i = new Intent(TestNotesDurationActivity.this, MusicPreferences.class);
			startActivity(i);
			// Some feedback to the user
//			Toast.makeText(HelloPreferences.this,
//					"Here you can enter your user credentials.",
//					Toast.LENGTH_LONG).show();
			break;

		}
		return true;
	}
	public void onResume() {
		super.onResume();
		getPrefs();
	}
    
	
    public void playtwelveNotes(View view) {
    	notesToPlayTwelve = new byte[][]{C,Cs,D,Ds,E,F,Fs,G,Gs,A,As,B};
    	notesToPlay = new byte[][]{B,Cs,E,B,F,G};
    	byte []rhythm = new byte[]{1,2,4,3,3,3};
    	switch(view.getId()) {
    	case R.id.whole:
    		new PlayNotesThread(1, this, notesToPlayTwelve).start();
    		break;
    	case R.id.half:
    		new PlayNotesThread(2, this, notesToPlayTwelve).start();
    		break;
    	case R.id.third:
    		new PlayNotesThread(3, this, notesToPlayTwelve).start();
    		break;
    	case R.id.quarter:
    		new PlayNotesThread(4, this, notesToPlayTwelve).start();
    		break;
    	case R.id.eighth:
    		new PlayNotesThread(8, this, notesToPlayTwelve).start();
    		break;
    	case R.id.other:
    		new PlayNotesThread(rhythm, this, notesToPlay).start();
    		break;
    	}
    }
    private void getPrefs() {
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        shpr = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        try {
        	MusicCore.setBpm(Integer.parseInt(shpr.getString("bpm", "120")));
        } catch(NumberFormatException nfe) {
        	MusicCore.setBpm(120);
        }
    }
}